const {
	Controller
} = require('uni-cloud-router')
const uniID = require('uni-id')
module.exports = class GamingController extends Controller {

	//DM点击开始剧本后创建房间
	async start() {
		let {
			clientIds,
			scriptId,
			dmClientid
		} = this.ctx.data

		if (!scriptId) {
			return {
				code: 500,
				msg: '请选择剧本后再开始'
			}
		}
		if (!clientIds || clientIds.length == 0) {
			return {
				code: 500,
				msg: '请添加游戏设备后再开始剧本'
			}
		}
		try {
			let result = await this.service.gaming.start({
				clientIds,
				scriptId,
				dmClientid
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			console.error(e)
			return {
				code: 500,
				msg: e.message
			}
		}
	}

	//剧本开始后获取当前剧本的DM信息
	async getDmList() {
		let {
			roomId,
		} = this.ctx.data

		if (!roomId) {
			return {
				code: 500,
				msg: '没有查找到该房间'
			}
		}
		try {
			let result = await this.service.gaming.getDmList({
				roomId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}

	//通过房间id获取DM手册
	async getDmManual() {
		let {
			roomId
		} = this.ctx.data

		if (!roomId) {
			return {
				code: 500,
				msg: '没有查找到房间'
			}
		}
		try {
			let result = await this.service.gaming.getDmManual({
				roomId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}

	//通过房间id获取当前剧本的角色列表
	async getRoleList() {
		let {
			roomId
		} = this.ctx.data

		if (!roomId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.getRoleList({
				roomId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}

	//DM给玩家发送线索（游戏中）
	async sendClue() {
		let {
			roomId,
			clueId,
			playerIds
		} = this.ctx.data
		if (!roomId || !clueId || !playerIds || playerIds.length == 0) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.sendClue({
				roomId,
				clueId,
				playerIds
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}

	//DM撤回某一条线索（游戏中）
	async cancelClue() {
		let {
			roomId,
			clueId
		} = this.ctx.data
		if (!roomId || !clueId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.cancelClue({
				roomId,
				clueId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}

	//DM给玩家发送(道具，内容，结局)（游戏中）
	async sendItem() {
		let {
			roomId,
			itemId,
			playerIds
		} = this.ctx.data
		if (!roomId || !itemId || !playerIds || playerIds.length == 0) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.sendItem({
				roomId,
				itemId,
				playerIds
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}

	//DM撤回某一个(道具，内容，结局)（游戏中）
	async cancelItem() {
		let {
			roomId,
			itemId
		} = this.ctx.data
		if (!roomId || !itemId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.cancelItem({
				roomId,
				itemId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//修改当前房间的阶段ID
	async updatePhaseId() {
		let {
			roomId,
			phaseId
		} = this.ctx.data
		if (!roomId || !phaseId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.updatePhaseId({
				roomId,
				phaseId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//进入投凶阶段后创建问题（已存在不创建）
	async insertQuestion() {
		let {
			roomId,
			phaseId
		} = this.ctx.data
		if (!roomId || !phaseId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.insertQuestion({
				roomId,
				phaseId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//通过房间id和角色id（选择角色）
	async ChooseRole() {
		let {
			roomId,
			roleId,
			clientid
		} = this.ctx.data
		if (!roomId || !roleId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.ChooseRole({
				roomId,
				roleId,
				clientid
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//设备选择角色后，获取当前阶段的剧本内容
	async getScriptContent() {
		let {
			roomId
		} = this.ctx.data
		if (!roomId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.getScriptContent({
				roomId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//接收到DM发送线索的请求后获取当前角色的所有已接受的线索
	async getRoleClue() {
		let {
			roomId
		} = this.ctx.data
		if (!roomId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.getRoleClue({
				roomId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	//接收到DM发送线索的请求后获取当前角色的所有已接受的物品（道具，剧本，结局）
	async getRoleItem() {
		let {
			roomId
		} = this.ctx.data
		if (!roomId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.getRoleItem({
				roomId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//公开当前线索
	async cluePublic() {
		let {
			roomId,
			clueId
		} = this.ctx.data
		if (!roomId || !clueId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.cluePublic({
				roomId,
				clueId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//当前设备修改自己的准备状态
	async updateRoleStatus() {
		let {
			roomId,
			readyToNextPhase
		} = this.ctx.data
		if (!roomId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.updateRoleStatus({
				roomId,
				readyToNextPhase
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//保存该玩家关于投凶问题的回答
	async saveAnwser() {
		let {
			roomId,
			questionConfigId,
			anwser
		} = this.ctx.data
		if (!roomId || !questionConfigId || !anwser) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.saveAnwser({
				roomId,
				questionConfigId,
				anwser
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//玩家呼叫DM(取消呼叫)
	async callDM() {
		let {
			roomId,
			confirmOrCancel
		} = this.ctx.data
		if (!roomId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.callDM({
				roomId,
				confirmOrCancel
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//DM接受玩家的呼叫
	async callOK() {
		let {
			playerId
		} = this.ctx.data
		if (!playerId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.callOK({
				playerId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	// //进入搜证阶段获取线索地图发送给玩家
	async searchClue() {
		let {
			roomId,
			phaseId
		} = this.ctx.data
		if (!roomId || !phaseId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.searchClue({
				roomId,
				phaseId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//玩家退出房间
	async quitRoom() {
		let {
			roomId
		} = this.ctx.data
		if (!roomId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.quitRoom({
				roomId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//向DM发出交易申请
	async dealApply() {
		let {
			roomId,
			type,
			target,
			clueId,
			itemId,
			skillId,
			description
		} = this.ctx.data
		if (!roomId || !type || !target) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.dealApply({
				roomId,
				type,
				target,
				clueId,
				itemId,
				skillId,
				description
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//DM获取当前房间的所有交易信息
	async getDealList() {
		let {
			roomId
		} = this.ctx.data
		if (!roomId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.getDealList({
				roomId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//DM审核交易申请
	async auditDeal() {
		let {
			applyId,
			confirmOrCancel
		} = this.ctx.data
		if (!applyId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.auditDeal({
				applyId,
				confirmOrCancel
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//玩家搜证发送线索
	async userGetClue() {
		let {
			roomId,
			clueId
		} = this.ctx.data
		if (!roomId || !clueId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.userGetClue({
				roomId,
				clueId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//DM给玩家发送技能（游戏中）
	async sendSkill() {
		let {
			roomId,
			skillId
		} = this.ctx.data
		if (!roomId || !skillId) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.sendSkill({
				roomId,
				skillId
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	//用户解决交易申请（线索，物品）
	async solveDeal() {
		let {
			clueId,
			itemId,
			money,
			confirmOrCancel
		} = this.ctx.data
		if (!money) {
			return {
				code: 500,
				msg: '参数错误'
			}
		}
		try {
			let result = await this.service.gaming.solveDeal({
				clueId,
				itemId,
				money,
				confirmOrCancel
			})
			return {
				code: 200,
				result: result
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
}
